package com.ocean.service.edu.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocean.dao.edu.TeacherDao;
import com.ocean.dto.GCourseListDto;
import com.ocean.dto.TeacherDto;
import com.ocean.entity.edu.Teacher;
import com.ocean.entity.user.User;
import com.ocean.service.edu.TeacherService;
import com.ocean.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xk
 * @version 1.0
 */
@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherDao dao;

    @Override
    public R save(Teacher teacher) {
        if (dao.insert(teacher) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R delete(int id) {
        if (dao.delete(id) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R update(Teacher teacher) {
        if (dao.update(teacher) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }

    }

    @Override
    public R queryPage(int page, int size) {
        PageHelper.startPage(page, size);
        PageInfo<Teacher> pageInfo = new PageInfo<>(dao.selectAll());
        return R.ok(pageInfo);
    }

    @Override
    public R queryPageWhere(GCourseListDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getSize());
        PageInfo<Teacher> pageInfo = new PageInfo<>(dao.selectWhere(dto.getTname()));
        return R.ok(pageInfo);

    }

    @Override
    public R importData(MultipartFile file) {
        //1.验证上传的内容是否非空
        if (!file.isEmpty()) {

            try {
                //2.EasyExcel读取---集合
                List<Teacher> list = EasyExcel.read(file.getInputStream(), Teacher.class, new ReadListener() {
                    @Override
                    public void invoke(Object o, AnalysisContext analysisContext) {
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    }
                }).doReadAllSync();

                //3.批量新增
                if (dao.insertBatch(list) > 0) {
                    return R.ok();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    @Override
    public void exportData(HttpServletResponse response) {
        //1.设置下载的响应消息头
        response.setHeader("Content-Disposition", "attachment;filename=user_" + System.currentTimeMillis() + ".xlsx");
        //2.查询数据
        List<Teacher> list = dao.selectAll();
        //3.生成Excel 同时写出
        try {
            EasyExcel.write(response.getOutputStream(), Teacher.class).sheet("自动导出").doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public R queryEcharts() {
        //1.查询数据
        List<TeacherDto> list = dao.selectNum();
        //2.组装数据
        Map<String, ArrayList> map = new HashMap<>();
        map.put("politics", new ArrayList());
        map.put("num", new ArrayList());
        list.forEach(d -> {
            map.get("politics").add(d.getPolitics());
            map.get("num").add(d.getNum());
        });
        return R.ok(map);

    }

}
